package com.bknow.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.bknow.model.UserInfo;
@Component
public class MyInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		if(request.getRequestURI().contains("manager_login")||request.getRequestURI().contains("userlogin")) {
			return true;
		}else{
			// 获取session
			HttpSession session = request.getSession();
			UserInfo userInfo=(UserInfo)session.getAttribute("userInfo");
			System.out.println(userInfo.getuName());
			// 判断用户ID是否存在，不存在就跳转到登录界面
			if ("".equals(userInfo.getuName())||userInfo.getuName()!=null) {
				System.out.println("用户已登陆");
				session.setAttribute("managerUserInfo", session.getAttribute("managerUserInfo"));
				return true;
			} else {
				System.out.println(session.getAttribute("userInfo"));
				System.out.println("managerUserInfo"+session.getAttribute("managerUserInfo"));
				response.sendRedirect("http://localhost/article_manager_login.html");
				return false;
			}
		}
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

}
